<?php
/**
 * 证书管理  证书的维护编辑
 *
 *
 */
class cert extends main
{
    const EFFECTIVE = 1;
    const CLOSEDOWN = 2;

    function __construct(){
        parent::__construct();
        $this->loadModel('g5_certificate');
		$this->loadModel('g5_perfile');
    }

    function index()
    {
        $page   = isset($_GET[3]) ? $_GET[3] : 1;
        $pageSize = 20;
        $where = $search = $like = array();
        $search = isset($_POST['s']) ? $_POST['s'] : array();
        $string = isset($_GET[2]) ? $_GET[2] : '';
        if($string != ''){
            $search = unserialize(base64_decode($string));
        }

		if( ! empty($search['appName'])){
            $like['name'] = trim($search['appName']);
        }
        if(!empty($search['account'])){
            $like['account'] = $search['account'];
        }

		if(!empty($search['status'])){
			$where['status'] = $search['status'];
        }
        $this->search = $search;
        $result = $this->g5_certificate->like($like)->limit($pageSize, ($page-1)*$pageSize)->by('id')->getAll('*', $where);
		//echo $this->g5_certificate->last_query();
        foreach($result as $key=>$val){
			$p_where['sourceCertificate'] = trim($val['id']);
			$result[$key]['perfile'] = $this->g5_perfile->getAll('*',$p_where);
			$result[$key]['p_count'] = count($result[$key]['perfile']);
		}
        $total  = $this->g5_certificate->like($like)->getCount('*', $where);
        $this->page($page, $total, base64_encode(serialize($search)), $pageSize);
        $this->result = $result;
        $this->proUse = gamesign::_getSignType();
        $this->status = $this->_status();
		$this->moreAction = BASEURI.'cert/more';
    }

	function info(){
		$c_id = isset($_GET[2]) ? $_GET[2] : 0;
		if($c_id){
			$where['id'] = trim($c_id);
			$this->certificate = $this->g5_certificate->getOne('*',$where);
		}
		$this->formAction = BASEURI.'cert/save';		
	}
	
	function save(){
		if(!empty($_POST)){
			//$add['name'] = $_POST['name'];
			//$add['md5'] = $_POST['md5'];
			//$add['status'] = $_POST['status'];
			$add['account'] = $_POST['account'];
			$add['mailPwd'] = $_POST['mailPwd'];
			$add['developerPwd'] = $_POST['developerPwd'];
			$add['phone'] = $_POST['phone'];
			$add['mailSecurity1'] = $_POST['mailSecurity1'];
			$add['mailAnswer1'] = $_POST['mailAnswer1'];
			$add['mailSecurity2'] = $_POST['mailSecurity2'];
			$add['mailAnswer2'] = $_POST['mailAnswer2'];
			$add['mailSecurity3'] = $_POST['mailSecurity3'];
			$add['mailAnswer3'] = $_POST['mailAnswer3'];
			$add['developerSecurity1'] = $_POST['developerSecurity1'];
			$add['developerAnswer1'] = $_POST['developerAnswer1'];
			$add['developerSecurity2'] = $_POST['developerSecurity2'];
			$add['developerAnswer2'] = $_POST['developerAnswer2'];
			$add['developerSecurity3'] = $_POST['developerSecurity3'];
			$add['developerAnswer3'] = $_POST['developerAnswer3'];
			$add['payTime'] = strtotime($_POST['payTime']);
			$add['source'] = $_POST['source'];
			if($_POST['id'] != ''){
				if($this->g5_certificate->update($add,array('id'=>$_POST['id']))){
					$this->report(true, BASEURI.'cert/index');
				}else{
					$this->report(false, BASEURI.'cert/index');
				}
			}else{
				$add['addUser'] = $_SESSION['adminmanager']['user_true_name'];
				if($this->g5_certificate->insert($add)){
					$this->report(true, BASEURI.'cert/index');
				}else{
					$this->report(false, BASEURI.'cert/index');
				}
			}
			
			
			
			
		}else{
			$this->report(false, BASEURI.'cert/index');
		}
	}

    /**
     * 检测证书
     */
    function check()
    {
        $this->g5_certificate->checkCer();
        $this->report(true, BASEURI.'cert/index');
    }

	//查看安全信息
	function more(){
		$c_id = $_POST['id'];
		$where['id'] = trim($c_id);
		$certificate = $this->g5_certificate->getOne('*',$where);
		foreach($certificate as $key=>$val){
			if(!$val){
				$certificate[$key] = '';
			}elseif($key == 'payTime'){
				$certificate['payTime'] = date('Y-m-d H:i:s',$certificate['payTime'] );
			}
		}
		
		echo json_encode($certificate);
		exit;
	}

    function _status()
    {
        return  array(
            self::EFFECTIVE  => '有效',
            self::CLOSEDOWN  => '被封'
        );

    }
}

